/**
 * UserManager.java
 * @author taowei20061122@163.com
 * 2013-6-9 下午5:51:23
 */
package study.designpattern.proxy;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import study.designpattern.proxy.bean.UserModel;
import study.designpattern.proxy.impl.Proxy;

/**
 * 实现实例要求的功能
 * @author taowei20061122@163.com
 * 2013-6-9 下午5:51:23
 */
public class UserManager {
	
	public List<UserModelApi> getUserByDepId(String depId) {
		List<UserModelApi> list = new ArrayList<UserModelApi>();
		Connection conn = Proxy.getConnection();
		String sql = "select userid,name from t_user_test where depid=?";
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, "1");
			rs = pst.executeQuery();
			while (rs.next()) {
				//这里创建的是代理对象，而不是直接创建UserModel对象
				Proxy proxy = new Proxy(new UserModel());
				//只是设置userId和name两个值就可以了
				proxy.setUserId(rs.getString("userid"));
				proxy.setName(rs.getString("name"));
				list.add(proxy);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			Proxy.close(rs, pst, conn);
		}
		return list;
	}
}
